package com.lu.servlet;

import com.lu.pojo.Type;
import com.lu.service.impl.TypeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
@WebServlet(name = "TypeServlet",urlPatterns = {"/Type"})
public class TypeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        PrintWriter out = resp.getWriter();

        String opr = req.getParameter("opr");
        if ("getAll".equals(opr)) {
            List<Type> typeList = new TypeServiceImpl().getAllType();
            req.getSession().setAttribute("typeList", typeList);
            req.getRequestDispatcher("typeList.jsp").forward(req, resp);
        } else if ("add".equals(opr)) {
            String tname = req.getParameter("tname");
            String created_By = req.getParameter("created_By");
            int i = new TypeServiceImpl().addOneType(new Type(tname, created_By));
            if (i > 0) {
                out.print("<script>alert('添加成功!');location.href = 'admin_0.jsp';</script>");
            } else {
                out.print("<script>alert('添加失败!');history.back();</script>");
            }
        } else if ("edit".equals(opr)) {
            int tid = Integer.parseInt(req.getParameter("tid"));
            Type type = new TypeServiceImpl().getOneType(tid);
            req.getSession().setAttribute("type", type);
            req.getRequestDispatcher("editType.jsp").forward(req, resp);
        } else if ("editSave".equals(opr)) {
            int tid = Integer.parseInt(req.getParameter("tid"));
            String update_By = req.getParameter("updated_By");
            String tname = req.getParameter("tname");
            int i = new TypeServiceImpl().updateOneType(new Type(tid, tname, update_By));
            if (i > 0) {
                out.print("<script>alert('修改成功!');location.href = 'Type?opr=getAll';</script>");
            } else {
                out.print("<script>alert('修改失败!');history.back();</script>");
            }
        } else if ("del".equals(opr)) {
            int tid = Integer.parseInt(req.getParameter("tid"));
            int i = new TypeServiceImpl().delOneType(tid);
            if (i > 0) {
                out.print("<script>alert('删除成功!');location.href = 'Type?opr=getAll';</script>");
            } else {
                out.print("<script>alert('删除失败!');history.back();</script>");
            }
        }

        out.flush();
        out.close();
    }
}
